package org.mtdownload.app;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.mtdownload.download.DownloadManager;
import org.mtdownload.utils.PreferencesManager;

public class MTDownload {

	public MTDownload() {
		init();
	}
	
	private void init(){
		MTDownloadMainFrame frame=new MTDownloadMainFrame("MultiThread Download"); 
		Dimension frameSize=frame.getSize();		
		Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
		if(frameSize.height>screenSize.height){
			frameSize.height=screenSize.height;
		}
		if(frameSize.width>screenSize.width){
			frameSize.width=screenSize.width;
		}
		Point defaultLocation= PreferencesManager.getFrameLocation();
		if(defaultLocation==null){
			frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
		}else{
			frame.setLocation(defaultLocation);
		}
		
		frame.setVisible(true);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Runtime.getRuntime().addShutdownHook(new Thread(){
			public void run(){
				PreferencesManager.savePreferences();
				String databaseFile=PreferencesManager.getDownloadListDatabase();
				DownloadManager.stopAllDownloads();
				DownloadManager.saveDownloadList(databaseFile);				
			}
		});		
		try{
			
			//the 1.5 way of setting anti-aliased text
			System.getProperties().put("swing.aatext","true");
			UIManager.setLookAndFeel(new de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel());
			/*String osName=System.getProperty("os.name");
			if(osName.toLowerCase().indexOf("windows")>=0){				
				//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				
			}else{
				UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticLookAndFeel");
				//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());				
			}			*/
		}catch(Throwable exc){
			exc.printStackTrace();
		}
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				new MTDownload();	
			}
		});				
	}
	
}
